package com.hoyo.web;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.hoyo.common.PageInfo;
import com.hoyo.domain.Catalog;
import com.hoyo.domain.CatalogCollect;
import com.hoyo.domain.CatalogReport;
import com.hoyo.service.CatalogService;
import com.hoyo.domain.User;

@Controller
public class CatalogController {
	
	@Autowired
	private CatalogService catalogService;
	
	@RequestMapping("/admin/catalog/notCheck")
	@ResponseBody
	public String notCheckCatalogs(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize")int pageSize){
		JSONObject obj = new JSONObject();
		try {
			PageInfo<Catalog> pageInfo = catalogService.pageFindCatalogsByChecked(false, pageNum, pageSize);
			obj.put("success", true);
			obj.put("pageInfo", pageInfo);
		} catch (Exception e) {
			obj.put("success", false);
			e.printStackTrace();
		}
		return obj.toString();
	}
	
	@RequestMapping("/admin/catalog/checkPass")
	@ResponseBody
	public String checkPass(@RequestParam("id") int id){
		JSONObject obj = new JSONObject();
		try {
			catalogService.enabledCatalog2show(id);
			PageInfo<Catalog> pageInfo = catalogService.pageFindCatalogsByChecked(false, 1, 3);
			obj.put("success", true);
			obj.put("pageInfo", pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		return obj.toString();
	}
	
	@RequestMapping("/admin/catalog/checkNotPass")
	@ResponseBody
	public String checkNotPass(@RequestParam("id") int id){
		JSONObject obj = new JSONObject();
		try {
			catalogService.enabledCatalog2show(id);
			obj.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		return obj.toString();
	}
	
	@RequestMapping("/admin/catalog/catalogs")
	@ResponseBody
	public String catalogs(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize){
		JSONObject obj = new JSONObject();
		try {
			PageInfo<Catalog> pageInfo = catalogService.pageFindCatalogs(pageNum, pageSize);
			obj.put("pageInfo", pageInfo);
			obj.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		return obj.toString();
	}
	
	@RequestMapping("/admin/channel/catalogs")
	@ResponseBody
	public String catalogs4channel(@RequestParam("channelId") int channelId, @RequestParam("pageNum") int pageNum
			, @RequestParam("pageSize") int pageSize){
		JSONObject obj = new JSONObject();
		try {
			PageInfo<Catalog> pageInfo = catalogService.pageFindCatalogsByChannelId(channelId, pageNum, pageSize);
			obj.put("pageInfo", pageInfo);
			obj.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		return obj.toString();
	}
	
	@RequestMapping("/admin/catalog/offLoad")
	@ResponseBody
	public String offLoadCatalog(@RequestParam("catalogId") int catalogId){
		JSONObject obj = new JSONObject();
		try {
			PageInfo<Catalog> pageInfo = catalogService.offLoadCatalog(catalogId);
			obj.put("pageInfo", pageInfo);
			obj.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		return obj.toString();
	}
	
	@RequestMapping("/admin/catalog/onLoad")
	@ResponseBody
	public String onLoadCatalog(@RequestParam("catalogId") int catalogId){
		JSONObject obj = new JSONObject();
		try {
			PageInfo<Catalog> pageInfo = catalogService.onLoadCatalog(catalogId);
			obj.put("pageInfo", pageInfo);
			obj.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		return obj.toString();
	}
	
	@RequestMapping("/admin/catalog/remove")
	@ResponseBody
	public String removeCatalog(@RequestParam("catalogId") int catalogId){
		JSONObject obj = new JSONObject();
		try {
			PageInfo<Catalog> pageInfo = catalogService.removeCatalog(catalogId);
			obj.put("pageInfo", pageInfo);
			obj.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		return obj.toString();
	}
	
	@RequestMapping("/admin/catalog/search")
	@ResponseBody
	public String searchCatalog(@RequestParam("keyword") String keyword, @RequestParam("pageNum") int pageNum
			, @RequestParam("pageSize") int pageSize){
		JSONObject obj = new JSONObject();
		try {
			PageInfo<Catalog> pageInfo = catalogService.pageSearch(keyword, pageNum, pageSize);
			obj.put("pageInfo", pageInfo);
			obj.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		return obj.toString();
	}
	
	
	@RequestMapping("/user/catalog/shoucang")
	@ResponseBody
	public String shoucangCatalog(@RequestParam("catalogId") int catalogId,HttpSession session){
		JSONObject obj = new JSONObject();
		try {
			catalogService.shoucangCatalogsById(new CatalogCollect(((User)session.getAttribute("user")).getId(),catalogId));
			obj.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		return obj.toString();
	}
	
	
	@RequestMapping("/user/catalog/cancelshoucang")
	@ResponseBody
	public String cancelshoucang(@RequestParam("catalogId") int catalogId,HttpSession session){
		JSONObject obj = new JSONObject();
		try {
			catalogService.cancelshoucangCatalogsById(catalogId, ((User)session.getAttribute("user")).getId());
			obj.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		return obj.toString();
	}
	
	@RequestMapping("/user/catalog/report")
	@ResponseBody
	public String catalogReport(@RequestParam("catalogId") int catalogId,@RequestParam("type") String type,@RequestParam("description") String description,HttpSession session){
		JSONObject obj = new JSONObject();
		try {
			catalogService.saveCatalogReport(new CatalogReport(((User)session.getAttribute("user")).getId(), catalogId, type, description));
			obj.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		return obj.toString();
	}
	
	@RequestMapping("/admin/catalog/beReported")
	@ResponseBody
	public String reportedCatalogs(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize){
		JSONObject obj = new JSONObject();
		try {
			PageInfo<CatalogReport> pageInfo = catalogService.pageFindCatalogReports(pageNum, pageSize);
			obj.put("success", true);
			obj.put("pageInfo", pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		return obj.toString();
	}
	
	@RequestMapping("/admin/catalog/reportPass")
	@ResponseBody
	public String reportPass(@RequestParam("id") int id, @RequestParam("catalogId") int catalogId){
		JSONObject obj = new JSONObject();
		try {
			PageInfo<CatalogReport> pageInfo = catalogService.reportPass(id, catalogId);
			obj.put("pageInfo", pageInfo);
			obj.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		return obj.toString();
	}
	
	@RequestMapping("/admin/catalog/reportNotPass")
	@ResponseBody
	public String reportNotPass(@RequestParam("id") int id){
		JSONObject obj = new JSONObject();
		try {
			PageInfo<CatalogReport> pageInfo = catalogService.reportNotPass(id);
			obj.put("pageInfo", pageInfo);
			obj.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		return obj.toString();
	}
}












